import sympy
import numpy as np
from jili.core.printlog import print
def calcor(**kwargs):
    """

    Args:
        **kwargs:
        out:r
        FV:
        PV:
        pv0:
        MS:montly supply
        YS:
        r:
        mr:
        dr:
        n:
    Returns:

    """
    if "out" not in kwargs.keys():
        return None
    else:
        outtype=kwargs["out"]
        if outtype in ["FV","fv"]:
            n=1
            if "n" in kwargs.keys():
                n=kwargs["n"]
            r=0.1
            if "r" in kwargs.keys():
                r=kwargs["r"]
            elif "mr" in kwargs.keys():
                r=kwargs["mr"]*12
            elif "dr" in kwargs.keys():
                r=kwargs["dr"]*365
            pv=0
            if "PV" in kwargs.keys():
                pv=kwargs["PV"]
            elif "pv" in kwargs.keys():
                pv=kwargs["pv"]
            return pv*(1+r)**n
        elif outtype in ["PV","pv"]:
            n = 1
            if "n" in kwargs.keys():
                n = kwargs["n"]
            r = 0.1
            if "r" in kwargs.keys():
                r = kwargs["r"]
            elif "mr" in kwargs.keys():
                r = kwargs["mr"] * 12
            elif "dr" in kwargs.keys():
                r = kwargs["dr"] * 360
            fv = 0
            if "FV" in kwargs.keys():
                fv = kwargs["FV"]
            elif "fv" in kwargs.keys():
                fv = kwargs["fv"]
            return fv /((1 + r) ** n)
        elif outtype in ["MS","ms"]:
            mn = 12
            if "n" in kwargs.keys():
                mn = kwargs["n"]
            mr = 0.1/12
            if "r" in kwargs.keys():
                mr = kwargs["r"]/12
            elif "mr" in kwargs.keys():
                mr = kwargs["mr"]
            elif "dr" in kwargs.keys():
                mr = kwargs["dr"] * 30
            pv = 0
            if "PV" in kwargs.keys():
                pv = kwargs["PV"]
            elif "pv" in kwargs.keys():
                pv = kwargs["pv"]
            elif "fv" in kwargs.keys():
                fv = kwargs["fv"]
                pv = calcor(FV=fv,r=mr*12,n=mn/12)
            elif "FV" in kwargs.keys():
                fv = kwargs["FV"]
                pv = calcor(FV=fv,r=mr*12,n=mn/12)
            x = sympy.Symbol('x')
            fx = -pv
            for ii in range(mn):
                i = ii + 1
                fx = fx+x/((1+mr)**i)
            rst = sympy.solve(fx, x)
            return rst[0]
        elif outtype in ["mr","MR","R","r"]:
            mn = 12
            if "n" in kwargs.keys():
                mn = kwargs["n"]
            ms = 0
            if "ms" in kwargs.keys():
                ms = kwargs["ms"]
            pv = 0
            if "pv0" in kwargs.keys():
                pv=kwargs["pv0"]
            x = sympy.Symbol('x')
            if "PV" in kwargs.keys():
                pv = pv-kwargs["PV"]
            elif "pv" in kwargs.keys():
                pv = pv-kwargs["pv"]
            elif "fv" in kwargs.keys():
                fv = kwargs["fv"]
                pv = pv-calcor(FV=fv, r=x * 12, n=mn / 12)
            elif "FV" in kwargs.keys():
                fv = kwargs["FV"]
                pv = pv-calcor(FV=fv, r=x * 12, n=mn / 12)
            fx = [pv]
            for ii in range(mn):
                fx.append(ms)
            #print(fx)
            irr=np.irr(fx)
            #print(rst)
            if outtype in ["mr","MR"]:
                return irr
            else:
                return (1+irr)**12-1
if __name__=="__main__":
    pv=152800
    ms12=10911
    ms24=5805
    ms36=4112
    pv0=30600
    print("12 mr:",str(100*calcor(out="mr",pv=pv,pv0=pv0,ms=ms12,n=12))+"%",str(100*calcor(out="r",pv=pv,pv0=pv0,ms=ms12,n=12))+"%")
    print("24 mr:", str(100*calcor(out="mr", pv=pv,pv0=pv0, ms=ms24,n=24))+"%",str(100*calcor(out="r", pv=pv,pv0=pv0, ms=ms24,n=24))+"%")
    #print("36 mr:", calcor(out="mr", pv0=pv0, ms=ms36,n=36))
